package com.bawei.shixun.view.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bawei.newsapplication.R
import com.bawei.coom.model.bean.fomm.NewsListBean
import com.bumptech.glide.Glide
class LeiAdapter(val context: Context, val list: List<NewsListBean.Result>) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val vierw: View


//        for (result in list) {
//            Log.i("jjjjjjjjjjj", result.title)
//        }


        return when (viewType) {
            0 -> {
                vierw = LayoutInflater.from(context).inflate(R.layout.fronpagelist_item, parent, false)
                ViewHolder1(vierw)
            }

            1 -> {
                vierw = LayoutInflater.from(context).inflate(R.layout.frontitle_item, parent, false)
                ViewHolder2(vierw)
            }

            else -> {
                vierw = LayoutInflater.from(context).inflate(R.layout.pagelistttitle_item, parent, false)
                ViewHolder3(vierw)
            }
        }
    }
    override fun getItemCount(): Int {
        return list.size
    }

    lateinit  var split: List<String>

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        split = ArrayList<String>()
        (split as ArrayList<String>).clear()
        if (list[position].thumbnailUrl != null) {
            split = list[position].thumbnailUrl.split(";")
        }
        if (split.size==0){
            if (list[position].thumbnailUrl != null) {
                split = list[position].thumbnailUrl.split("?")
            }
        }
        when (holder) {
            is ViewHolder1 -> {
                (holder as ViewHolder1).textview.text = list[position].title
            }

            is ViewHolder2 -> {
                (holder as ViewHolder2).textview.text = list[position].title
                Glide.with(context)
                    .load(split.get(0))
                    .into((holder as ViewHolder2).image1)
            }

            is ViewHolder3 -> {
                (holder as ViewHolder3).textview.text = list[position].title
                Glide.with(context)
                    .load(split[0])
                    .into((holder as ViewHolder3).image1)
                Glide.with(context)
                    .load(split[1])
                    .into((holder as ViewHolder3).image2)
                Glide.with(context)
                    .load(split[2])
                    .into((holder as ViewHolder3).image3)

            }
        }
    }
    override fun getItemViewType(position: Int): Int {
        if (list[position].thumbnailUrl != null) {
            var let2 = list[position].thumbnailUrl.split(";")
            return let2.size
        } else {
            return 0
        }

    }
}


class ViewHolder1(itemView: View) : RecyclerView.ViewHolder(itemView) {

    val textview: TextView = itemView.findViewById(R.id.front_name)
}

class ViewHolder2(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val textview: TextView = itemView.findViewById(R.id.frontlist_name)
    val image1: ImageView = itemView.findViewById(R.id.fronlist_img1)
}

class ViewHolder3(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val textview: TextView = itemView.findViewById(R.id.fronpage_name3)
    val image1: ImageView = itemView.findViewById(R.id.fronpage_img)
    val image2: ImageView = itemView.findViewById(R.id.fronpage_img2)
    val image3: ImageView = itemView.findViewById(R.id.fronpage_img3)
}